package com.example.programming.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.programming.sendemail.EmailController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;

import com.example.programming.common.Result;
import com.example.programming.sys.mapper.UserMapper;
import com.example.programming.sys.entity.User;

import org.springframework.web.bind.annotation.RestController;
import com.example.programming.common.tools.BaseController;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器 chrimer
 * </p>
 *
 * @author chrimer
 * @since 2020-06-25
 */
@RestController
@RequestMapping("/sys/user")
public class UserController extends BaseController {
    @Autowired
    @Resource
    private UserMapper userMapper;
    private String code;

    @RequestMapping(method= RequestMethod.POST,value="/getyzm")
    public Result getyzm(String nickname,String email){
        EmailController emailController = new EmailController();
        Result result = new Result();
        User user = new User();
        user.setNickname(nickname);
        user.setEmail(email);
        User userExit = userMapper.selectOne(new QueryWrapper<User>(user));
        if(userExit == null)
        {
            result.fail("账号或者邮箱不正确");
        }
        else
        {
            code = emailController.sendEmail(email);
            if(emailController.sendEmail(email)==null)
            {
                result.fail("验证码发送失败");
            }
            else
            {
                result.success("验证码发送成功");
            }
        }
        return result;
    }

    @RequestMapping(method= RequestMethod.POST,value="/resetpassword")
    public Result resetpassword(String nickname,
                                String yzm,
                                String password){
        Result result = new Result();
        if(yzm.equals(code))
        {
            userMapper.updatepassword(nickname,password);
            result.success("密码重置成功");
        }
        else {
            result.fail("验证码错误");
        }
        return result;
    }

    @RequestMapping(method= RequestMethod.POST,value="/login")
    public Result login(String username, String password){
        System.out.println("username："+username+"password:"+password);
        Result result = new Result();

        User user = new User();
        user.setNickname(username);
        user.setPassword(password);

        User userExit = userMapper.selectOne(new QueryWrapper<User>(user));
        System.out.println("测试");
        if (userExit == null) {

            result.fail("用户名或密码错误!");
        } else {
            System.out.println("userMapper.getuseful(username)="+userMapper.getuseful(username));
            if(userMapper.getuseful(username) == 1)
            {
                result.setData(userExit);
                result.success("登录成功");
            }
            else
            {
                result.fail("该用户被禁用！");
            }
        }
        return result;

    }

    @RequestMapping(method= RequestMethod.POST,value="/page")
    public Result page(
        Integer pageNum,
        Integer pageSize) {
        Result result = new Result();
        Page<User> page = new Page<User>(pageNum, pageSize);
        User parms = new User();
        QueryWrapper<User> warpper = new QueryWrapper<User>(parms);

        result.setData(userMapper.selectPage(page, warpper));
        return result;
    }
    @RequestMapping(method= RequestMethod.POST,value="/save")
    public Result save(@RequestBody User entity) {
        Result result = new Result();
        if (entity.getId() == null) {
            if(userMapper.user(entity.getNickname())==null)
            {
                entity.setUseful(1);
                entity.setDel(0);
                userMapper.insert(entity);
                result.success("注册成功");
            }
            else
            {
                result.fail("此账号已被使用");
            }
        }else {
            if(userMapper.user(entity.getNickname())==null)
            {
                userMapper.updateById(entity);
                result.success("修改成功");
            }
            else
            {
                result.fail("此账号已被使用");
            }
        }
        return result;
    }
    @RequestMapping(method= RequestMethod.DELETE,value="/delete")
    public Result delete( String ids) {
        Result result = new Result();
        List<String> deleteIds = new ArrayList<String>();
        for (String id : ids.split(",")) {
            deleteIds.add(id);
        }
        userMapper.deleteBatchIds(deleteIds);
        return result;
    }

    @RequestMapping(method= RequestMethod.POST,value="/studentList")
    public Result studentList(
            Integer pageNum,
            Integer pageSize,
            String nickname,String id) {
        Result result = new Result();
        Page<User> page = new Page<User>(pageNum, pageSize);
        User parms = new User();
        QueryWrapper<User> wrapper = new QueryWrapper<User>(parms);
        if(nickname!=null&&!"".equals(nickname)){
            wrapper.like("nickname",nickname);
        }
        if(id!=null&&!"".equals(id)){
            wrapper.like("id",id);
        }
        wrapper.eq("Identity_type","1");
        result.setData(userMapper.selectPage(page, wrapper));
        return result;
    }

    @RequestMapping(method= RequestMethod.POST,value="/teacherList")
    public Result teacherList(
            Integer pageNum,
            Integer pageSize,String id,String nickname) {
        Result result = new Result();
        Page<User> page = new Page<User>(pageNum, pageSize);
        User parms = new User();
        QueryWrapper<User> wrapper = new QueryWrapper<User>(parms);
        if(nickname!=null&&!"".equals(nickname)){
            wrapper.like("nickname",nickname);
        }
        if(id!=null&&!"".equals(id)){
            wrapper.like("id",id);
        }
        wrapper.eq("Identity_type","0");
        result.setData(userMapper.selectPage(page, wrapper));
        return result;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/studentAdd")
    public Result studentAdd(String nickname, String password, String email, String tel,
                             String sex, String birthday, Integer identityType, String school,
                             String education, String curriculumVitae, String useful, Integer del) {
        Result result = new Result();
        User user = new User();
        Integer studentSex, studentUseful;
        if ("男".equals(sex)) {
            studentSex = 0;
        } else {
            studentSex = 1;
        }
        if ("启用".equals(useful)) {
            studentUseful = 1;
        } else {
            studentUseful = 0;
        }

        user.setNickname(nickname);
        user.setPassword(password);
        user.setEmail(email);
        user.setTel(tel);
        user.setSex(studentSex);
        user.setBirthday(birthday);
        user.setIdentityType(identityType);
        user.setSchool(school);
        user.setEducation(education);
        user.setCurriculumVitae(curriculumVitae);
        user.setUseful(studentUseful);
        user.setDel(del);

        if (userMapper.selectOne(new QueryWrapper<User>(user)) == null) {
            userMapper.insert(user);
            result.success("添加成功");
        } else {
            result.fail("该用户已存在");
        }
        return result;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/studentDelete")
    public Result studentDelete(String id) {
        Result result = new Result();
        User have = new User();
        have.setId(id);
        QueryWrapper wrapper = new QueryWrapper<User>(have);
        userMapper.delete(wrapper);
        result.success("删除成功");
        return result;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/teacherAdd")
    public Result teacherAdd(String nickname, String password, String email, String tel,
                             String sex, String birthday, Integer identityType, String school,
                             String education, String curriculumVitae, String useful, Integer del
    ) {
        Result result = new Result();
        User user = new User();
        Integer studentSex, studentUseful;
        if ("男".equals(sex)) {
            studentSex = 0;
        } else {
            studentSex = 1;
        }
        if ("启用".equals(useful)) {
            studentUseful = 1;
        } else {
            studentUseful = 0;
        }

        user.setNickname(nickname);
        user.setPassword(password);
        user.setEmail(email);
        user.setTel(tel);
        user.setSex(studentSex);
        user.setBirthday(birthday);
        user.setIdentityType(identityType);
        user.setSchool(school);
        user.setEducation(education);
        user.setCurriculumVitae(curriculumVitae);
        user.setUseful(studentUseful);
        user.setDel(del);

        if (userMapper.selectOne(new QueryWrapper<User>(user)) == null) {
            userMapper.insert(user);
            result.success("添加成功");
        } else {
            result.fail("该用户已存在");
        }
        return result;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/teacherDelete")
    public Result teacherDelete(String id) {
        Result result = new Result();
        User user = new User();
        user.setId(id);
        QueryWrapper wrapper = new QueryWrapper<User>(user);
        userMapper.delete(wrapper);
        result.success("删除成功");
        return result;
    }

    @RequestMapping(method= RequestMethod.POST,value="/administList")
    public Result administList(
            Integer pageNum,
            Integer pageSize,String nickname) {
        Result result = new Result();
        Page<User> page = new Page<User>(pageNum, pageSize);
        User parms = new User();
        QueryWrapper<User> wrapper = new QueryWrapper<User>(parms);

        wrapper.eq("nickname",nickname);
        System.out.println("yonghuming:" + nickname);
        wrapper.eq("Identity_type","2");
        result.setData(userMapper.selectPage(page, wrapper));
        return result;
    }

    @RequestMapping(method= RequestMethod.POST,value="/administUpdate")
    public Result administUpdate(@RequestBody User entity) {
        Result result = new Result();
        System.out.println("id:"+entity.getId());
        if (entity.getId() != null) {
            userMapper.updateById(entity);
        }
        return result;
    }

    @RequestMapping(method= RequestMethod.POST,value="/studentUpdate")
    public Result studentUpdate(@RequestBody User entity) {
        Result result = new Result();
        System.out.println("id:"+entity.getId());
        if (entity.getId() != null) {
            userMapper.updateById(entity);
        }
        return result;
    }

    @RequestMapping(method= RequestMethod.POST,value="/teacherUpdate")
    public Result teacherUpdate(@RequestBody User entity) {
        Result result = new Result();
        System.out.println("id:"+entity.getId());
        if (entity.getId() != null) {
            userMapper.updateById(entity);
        }
        return result;
    }
    @RequestMapping(method= RequestMethod.GET,value="/message")
    public Result message(String name) {
        Result result = new Result();
        User parms = new User();
        QueryWrapper<User> warpper = new QueryWrapper<User>(parms);
        System.out.println("name:"+name);
        result.setData(userMapper.selectOne(warpper.eq("nickname",name)));
        return result;
    }
    @RequestMapping(method= RequestMethod.POST,value="/updateuser")
    public Result updateuser(@RequestBody User entity) {
        Result result = new Result();
        userMapper.updateById(entity);
        result.success("修改成功");
        return result;
    }
}
